home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT67.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  7.7 KB  |  274 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0
  4.                  Demonstration Program 67
  5.  
  6.   This scrolling example shows how to draw directly on the scrolling window.
  7.   It makes a reduced size map by creating 1x1 tiles with the most common
  8.   color found in each large tile.  This small window is displayed at the top
  9.   right corner, overtop the main scrolling window.  As well, a cross shows
  10.   the current viewing position in both windows.
  11.  
  12.  *** PROJECT ***
  13.  This program requires the WGT5_WC.LIB and WSCR_WC.LIB files to be linked.
  14.  
  15.  *** DATA FILES ***
  16.  MUN.WMP, MUNCHMAP.SPR, MUNCHKIN.SPR
  17.                                WATCOM C++ VERSION
  18. ==============================================================================
  19. */
  20. #include <dos.h>
  21. #include <malloc.h>
  22. #include <stdio.h>
  23. #include <conio.h>
  24. #include <time.h>
  25.  
  26. #include <wgt5.h>
  27. #include <wgtscrol.h>
  28.  
  29. typedef short tiletypes[256];
  30.  
  31. short movex, movey;
  32. short lookx, looky;
  33.  
  34. wgtmap munchmap;                   /* our world map */
  35. tiletypes munchtypes;
  36.  
  37. #define UP 72
  38. #define DOWN 80
  39. #define LEFT 75
  40. #define RIGHT 77
  41. #define CTRL 29
  42. #define ESC 1
  43.  
  44. #define TILE_SIZE 16
  45.  
  46. #define MAINWIN 0
  47. #define MINIWIN 1
  48. #define NUM_TILE 256
  49. #define NUM_OBJ 256
  50. #define NUM_SPR 100
  51.  
  52. color palette[256];                /* our palette of colours */
  53.  
  54. block munchtiles[NUM_TILE];        /* our blocks for the map */
  55. block minitiles[NUM_TILE];
  56. block munchsprites[NUM_SPR];       /* our sprites */
  57. scrollsprite munchobj[NUM_OBJ];
  58.  
  59. short windx, windy;
  60. short mwindx, mwindy;
  61.  
  62. short i;
  63.  
  64. short oldmode;
  65.  
  66.  
  67. short colfreq[256];
  68.  
  69. void make_mini_tiles (block *tiles)
  70. /* ---------------------------------------------------------------------------
  71.    (Originally written by Barry Egerter for the WGT Map Maker)
  72.    Scan through 256 color palette and our tiles to determine the best color
  73.    number to use for tile representation in the reduced map image.
  74.  
  75.    I simply find the most frequently occuring color in a given tile, and
  76.    store it in an array. Whenever I want to draw that tile on the small map
  77.    representation, use the color number given in the array.
  78. --------------------------------------------------------------------------- */
  79. {
  80.   unsigned short num;
  81.   unsigned short maxctr;
  82.   unsigned short mostfreq;
  83.   unsigned short pixelctr;
  84.   block tempbl;
  85.  
  86.   for (num = 0; num < NUM_TILE; num++)
  87.   /* Search through each tile */
  88.   {
  89.     if (tiles[num] != NULL)
  90.      /* The tile exists */
  91.     {
  92.       for (pixelctr = 0; pixelctr < 256; pixelctr++)
  93.      colfreq[pixelctr] = 0;
  94.       /* Clear out the frequency table */
  95.  
  96.       tempbl = tiles[num];
  97.       maxctr = wgetblockwidth (tempbl) * wgetblockheight (tempbl);
  98.       /* Holds the number of pixels in the tile */
  99.  
  100.       tempbl += 4;                            /* Make ptr to tile */
  101.       pixelctr = 0;                             /* set ctr to 0 */
  102.       do {
  103.     colfreq[*tempbl]++;                     /* Update frequency table */
  104.     tempbl++;
  105.     pixelctr++;
  106.       } while (pixelctr < maxctr);
  107.  
  108.       mostfreq = 0;
  109.       for (pixelctr = 0; pixelctr < 256; pixelctr++)    /* Scan for most common */
  110.     if (colfreq[pixelctr] >= colfreq[mostfreq])
  111.       mostfreq = pixelctr;
  112.  
  113.       wsetcolor (mostfreq);
  114.       wputpixel (0, 0);
  115.       minitiles[num] = wnewblock (0, 0, 0, 0);
  116.       /* Draw a single pixel and make a new mini tile */
  117.     }
  118.    else
  119.      minitiles[num] = NULL;
  120.   }
  121.  
  122. }
  123.  
  124.  
  125.  
  126.  
  127. void main (void)
  128. {
  129. int x, y;
  130.  
  131.   oldmode = wgetmode ();
  132.   if (!vgadetected ())
  133.   {
  134.     printf ("VGA is required to run this program...");
  135.     exit (1);
  136.   }
  137.  
  138.   printf ("WGT Example #67\n\n");
  139.   printf ("This scrolling example shows how to draw directly on the scrolling window.\n");
  140.   printf ("It makes a reduced size map by creating 1x1 tiles with the most common\n");
  141.   printf ("color found in each large tile.  This small window is displayed at the top\n");
  142.   printf ("right corner, overtop the main scrolling window.  As well, a cross shows\n");
  143.   printf ("the current viewing position in both windows.\n");
  144.  
  145.   printf ("\n\nWindow Width (2-20):");
  146.   scanf ("%i", &windx);
  147.   printf ("\nWindow Height (2-12):");
  148.   scanf ("%i", &windy);
  149.  
  150.   mwindx = 32;
  151.   mwindy = 32;
  152.  
  153.  
  154.   vga256 ();
  155.   wloadsprites (palette, "munchmap.spr", munchtiles, 0, NUM_TILE - 1);
  156.   wloadsprites (palette, "munchkin.spr", munchsprites, 0, NUM_SPR - 1);
  157.   wsetpalette (0, 255, palette);
  158.  
  159.   make_mini_tiles (munchtiles);
  160.  
  161.   winitscroll (MAINWIN, NORMAL, - 1, windx, windy, munchtiles);
  162.   munchmap = wloadmap (MAINWIN, "mun.wmp", munchtypes, munchobj);
  163.  
  164.   winitscroll (MINIWIN, NORMAL, - 1, mwindx, mwindy, minitiles);
  165.   wcopymap (0, 1);
  166.  
  167.   wnormscreen ();
  168.   wcls (0);
  169.  
  170.   wshowwindow (MAINWIN, 0, 0);
  171.   wshowwindow (MINIWIN, 0, 0);
  172.   lookx = 0;
  173.   looky = 0;
  174.  
  175.   installkbd ();
  176.  
  177.   do {
  178.  
  179.     movex = 0;
  180.     movey = 0;
  181.     
  182.     if (kbdon[LEFT])
  183.       movex = -8;
  184.     else if (kbdon[RIGHT])
  185.       movex = 8;
  186.     if (kbdon[UP])
  187.       movey = -8;
  188.     else if (kbdon[DOWN])
  189.       movey = 8;
  190.  
  191.     /* Lookx and looky are the coordinates of a moving object on the world */
  192.     /* The following code finds the difference between the current world  
  193.        coordinates and this object, and scrolls to center the object within
  194.        the window. */
  195.  
  196.     lookx += movex;
  197.     looky += movey;
  198.  
  199.     if (lookx < 0)
  200.       lookx = 0;
  201.     else if (lookx > mapwidth[MAINWIN] * 16)
  202.       lookx = mapwidth[MAINWIN] * 16;
  203.     
  204.     if (looky < 0)
  205.       looky = 0;
  206.     else if (looky > mapheight[MAINWIN] * 16)
  207.       looky = mapheight[MAINWIN] * 16;
  208.     
  209.  
  210.     /* Move the large window */
  211.     movex = lookx - worldx[MAINWIN] - windowmaxx[MAINWIN]/2 - 1;
  212.     movey = looky - worldy[MAINWIN] - windowmaxy[MAINWIN]/2 - 1;
  213.     wscrollwindow (MAINWIN, movex, movey);
  214.  
  215.     /* Move the small window.  Divide the movements by 16 since the tiles
  216.        are 1 pixel instead of 16. */
  217.     x = (lookx+1) / 16;
  218.     y = (looky+1) / 16;
  219.     movex = x - worldx[MINIWIN] - windowmaxx[MINIWIN]/2;
  220.     movey = y - worldy[MINIWIN] - windowmaxy[MINIWIN]/2;
  221.     wscrollwindow (MINIWIN, movex, movey);
  222.  
  223.     wshowobjects (MAINWIN, 1, NUM_OBJ - 1, munchsprites, munchobj);
  224.  
  225.     
  226.     /* Now we have two tiled maps shown in scrollblocks 0 and 1.
  227.        I want a cross to show the location of (lookx, looky) on both
  228.        maps.  First draw the cross on the mini map: */
  229.     wsetscreen (scrollblock[MINIWIN]);
  230.     /* Set drawing to the mini map */
  231.     
  232.     /* Get the screen coordinates of the cross */
  233.     x = wscreen_coordx (MINIWIN, x);    
  234.     y = wscreen_coordy (MINIWIN, y);    
  235.  
  236.     wsetcolor (1);
  237.     wline (x-1, y, x+1, y);  /* Draw a little cross */
  238.     wline (x, y-1, x, y+1);
  239.  
  240.     /* Now draw on the large map. */
  241.     wsetscreen (scrollblock[MAINWIN]);
  242.  
  243.     x = wscreen_coordx (MAINWIN, lookx);    
  244.     y = wscreen_coordy (MAINWIN, looky);    
  245.     
  246.     wline (x-5, y, x+5, y);  /* Draw a large cross */
  247.     wline (x, y-5, x, y+5);
  248.     
  249.     /* Paste the mini map over the large map, in the top right corner.
  250.        Leaves 5 pixels from the top right edge. */
  251.     wputblock (windx * 16 - mwindx - 5, 5, scrollblock[MINIWIN], NORMAL);
  252.     
  253.     /* Print out the coordinates of the cross */
  254.     wtextcolor (1);
  255.     wgtprintf (0, 0, NULL, "X: %hi  Y:%hi", lookx, looky);
  256.  
  257.     /* Return to the visual page and show the final image */
  258.     wnormscreen ();
  259.     wputblock (0, 0, scrollblock[MAINWIN], NORMAL);
  260.  
  261.   } while  (kbdon[ESC] != 1);          /* until ESC key is pressed */
  262.   
  263.   uninstallkbd ();
  264.   
  265.   wendscroll (MAINWIN);
  266.   wendscroll (MINIWIN);
  267.   wfreesprites (munchtiles, 0, NUM_TILE - 1);
  268.   wfreesprites (munchsprites, 0, NUM_SPR - 1);
  269.   wfreemap (munchmap);
  270.   wsetmode (oldmode);
  271. }
  272.  
  273.  
  274.